package 创建者模式.singleton.破坏单例模式.反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Description TODO
 * @Author shallow
 * @Date 2023/3/1 15:06
 */

public class Client {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//        1.获取字节码对象
        Class clazz = Singleton.class;
        //获得无参构造方法
        Constructor cons = clazz.getDeclaredConstructor();
        cons.setAccessible(true);//取消访问权限
        //创建Singleton对象
        Singleton instance1 = (Singleton) cons.newInstance();
        Singleton instance2 = (Singleton) cons.newInstance();
        //判断是否是同一对象，true为同一对象，没有破坏， false表示不是同一对象，破坏成功
        System.out.println(instance1 == instance2); //返回false破坏成功
    }
}
